package qqmeeting

import (
	"encoding/json"
	"errors"
	"fmt"
)

//QueryMeetingResponse 查询会议响应结果
type QueryMeetingResponse struct {
	Response
	MeetingNumber   int           `json:"meeting_number"`
	MeetingInfoList []MeetingInfo `json:"meeting_info_list"`
}

//QueryMeeting 查询会议，默认根据会议ID查询
func (c *Client) QueryMeeting(meetingID string, userid string, instanceid int) (resp QueryMeetingResponse, err error) {
	uri := fmt.Sprintf("/v1/meetings/%s?userid=%s&instanceid=%d", meetingID, userid, instanceid)

	return c.queryMeeting(uri)
}

//QueryMeetingCode 根据会议code查询
func (c *Client) QueryMeetingCode(meetingCode string, userid string, instanceid int) (resp QueryMeetingResponse, err error) {
	uri := fmt.Sprintf("/v1/meetings?meeting_code=%s&userid=%s&instanceid=%d", meetingCode, userid, instanceid)

	return c.queryMeeting(uri)
}

//queryMeetingCode 查询会议
func (c *Client) queryMeeting(uri string) (resp QueryMeetingResponse, err error) {
	var statusCode int
	var content []byte
	//请求
	if statusCode, content, err = c.GET(uri); err != nil {
		return
	}
	if content != nil {
		if err = json.Unmarshal(content, &resp); err != nil {
			err = errors.New(err.Error() + "\n" + string(content))
		}
	} else {
		err = fmt.Errorf("[%d] Bad request", statusCode)
	}
	resp.StatusCode = statusCode
	return
}
